package com.kotlin.hxh.ui.fragment.third.child

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.kotlin.hxh.R
import com.kotlin.hxh.base.BaseMainFragment
import com.kotlin.hxh.ui.fragment.third.child.child.ContentFragment
import com.kotlin.hxh.ui.fragment.third.child.child.MenuListFragment
import kotlinx.android.synthetic.main.toolbar.*


/**
 * @author huxh
 * @date 2018/12/20.
 */
class ShopFragment : BaseMainFragment() {
    companion object {
        @JvmStatic
        fun newInstance(): ShopFragment {
            val fragment = ShopFragment()
            fragment.arguments = Bundle()
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = inflater.inflate(R.layout.fragment_shop, container, false)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        toolbar.setTitle(R.string.shop)
        if (findChildFragment(MenuListFragment::class.java) == null) {
            val listMenus = arrayListOf<String>()
            listMenus.addAll(resources.getStringArray(R.array.array_menu).toList())
            val menuListFragment = MenuListFragment.newInstance(listMenus)
            loadRootFragment(R.id.fl_list_container, menuListFragment)
            loadRootFragment(R.id.fl_content_container, ContentFragment.newInstance(listMenus[0]))
        }
    }


    override fun onBackPressedSupport(): Boolean {
        // ContentFragment是ShopFragment的栈顶子Fragment,会先调用ContentFragment的onBackPressedSupport方法
        Toast.makeText(_mActivity, "onBackPressedSupport-->return false, " + getString(R.string.upper_process), Toast.LENGTH_SHORT).show()
        return false
    }

    /**
     * 替换加载 内容Fragment
     *
     * @param fragment
     */
    fun switchContentFragment(fragment: ContentFragment) {
        val findChildFragment = findChildFragment(ContentFragment::class.java)
        if (findChildFragment != null) {
            findChildFragment.replaceFragment(fragment, false)
        }
    }
}